﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class BLLStages
{
    DatabaseDataContext Context;

    public BLLStages()
	{
        Context = new DatabaseDataContext();
	}
    public Stage GetStageById(long id)
    {
        Stage result = null;
        try
        {
            result = (from s in Context.Stages where s.Id == id && s.IsEnabled == true select s).First();
        }
        catch { }
        return result;
    }
    public bool InsertTempDetails(String guid, long stageid)
    {
        bool result = false;
        try
        {
            var details = (from d in Context.SP_Insert_Assigned_Products_Temp(guid, stageid) select d).ToList();
        }
        catch
        {
            result = false;
        }
        return result;
    }
    public List<SP_Get_Unused_ProductsResult> GetUnassignedProducts(String guid, String filter, int startRow, int pageSize, string sortColumns)
    {
        List<SP_Get_Unused_ProductsResult> products = (from l in Context.SP_Get_Unused_Products(guid, filter, startRow, pageSize, sortColumns)
                            select l).ToList();
        return products;
    }
    public int GetUnassignedProductsRowCount(String guid, String filter)
    {
        int count = 0;
        List<SP_Get_Unused_Products_TotalResult> products = (from l in Context.SP_Get_Unused_Products_Total(guid, filter)
                                                       select l).ToList();
        if (products.Count > 0)
            count = products[0] == null ? 0 : Convert.ToInt32(products[0].total);
        return count;
    }
    public List<Stage_Products> GetAssignedProducts(String guid, int startRow, int pageSize, string sortColumns)
    {
        List<Stage_Products> products = (from l in Context.StageDetailsTemps join p in Context.Products on 
                                         l.ProductId equals p.Id where l.Guid == guid select new Stage_Products { id = l.ProductId, name = p.Name, 
                                             observation = l.Observation, quantity = l.Quantity}).ToList();
        if (sortColumns.Contains("DESC"))
        {
            if (sortColumns.Contains("name"))
                products = products.OrderByDescending(l => l.name).ToList();
        }
        else
        {
            if (sortColumns.Contains("name"))
                products = products.OrderBy(l => l.name).ToList();
        }
        return products.Skip(startRow).Take(pageSize).ToList();
    }
    public int GetAssignedProductsRowCount(String guid)
    {
        int count = 0;
        List<Stage_Products> products = (from l in Context.StageDetailsTemps
                                         join p in Context.Products on
                                             l.ProductId equals p.Id
                                         where l.Guid == guid
                                         select new Stage_Products
                                         {
                                             id = l.ProductId,
                                             name = p.Name,
                                             observation = l.Observation,
                                             quantity = l.Quantity
                                         }).ToList();
        count = products.Count();
        return count;
    }
    public bool Insert_Stage_Details_Temp(List<StageDetailsTemp> details)
    {
        bool result = true;
        List<int> groups = new List<int>();
        try
        {
            var toDelete = (from l in Context.StageDetailsTemps where l.CreatedDate < DateTime.Now.AddDays(-3) select l);
            Context.StageDetailsTemps.DeleteAllOnSubmit(toDelete);
            Context.SubmitChanges();
            Context.StageDetailsTemps.InsertAllOnSubmit(details);
            Context.SubmitChanges();
        }
        catch
        {
            result = false;
        }
        return result;
    }
    public bool Delete_Stage_Details_Temp(List<long> details, String guid, long stageId)
    {
        bool result = true;
        List<int> groups = new List<int>();
        try
        {
            var toDelete = (from l in Context.StageDetailsTemps where l.Guid == guid && l.StageId == stageId && details.Contains(l.ProductId) select l);
            Context.StageDetailsTemps.DeleteAllOnSubmit(toDelete);
            Context.SubmitChanges();
        }
        catch
        {
            result = false;
        }
        return result;
    }
    // 0 has quantities empties, 1 valid, 2 has no products selected
    public int isValidStageDetails(String uniqueId, long StageId)
    {
        int valid = 0;
        try
        {
            var to = (from s in Context.StageDetailsTemps where s.Guid == uniqueId && s.StageId == StageId select s);
            if (to.Count() > 0)
            {
                var total = (from s in Context.StageDetailsTemps where s.Guid == uniqueId && s.StageId == StageId && s.Quantity < 0 select s);
                if (total.Count() == 0)
                    valid = 1;
            }
            else
                valid = 2;
        }
        catch
        {
            valid = 0;
        }
        return valid;
    }
    public bool SaveStageWithDetails(Stage stage, String guid)
    {
        bool result = false;
        try
        {
            if (stage.Id > 0)
            {
                var st = (from s in Context.Stages where s.Id == stage.Id select s).First();
                st.Name = stage.Name;
                st.Observation = stage.Observation;
                st.IsEnabled = true;
                st.Temperature = stage.Temperature;
                st.Time = stage.Time;
                st.Water = stage.Water;
                st.Order = stage.Order;
            }
            else
            {
                Context.Stages.InsertOnSubmit(stage);
            }
            Context.SubmitChanges();
            var results = (from sd in Context.SP_Insert_Assigned_Products(guid, stage.Id, stage.CreatedBy) select sd).ToList();
            result = true;
        }
        catch
        {
            result = false;
        }
        return result;
    }
    public bool UpdateSelection(List<StageDetailsTemp> detailsTemp, String guid)
    {
        bool result = false;
        try
        {
            var ids = (from s in detailsTemp select s.ProductId).ToList();
            var toDelete = (from l in Context.StageDetailsTemps where l.Guid == guid && ids.Contains(l.ProductId) select l);
            Context.StageDetailsTemps.DeleteAllOnSubmit(toDelete);
            Context.SubmitChanges();
            Context.StageDetailsTemps.InsertAllOnSubmit(detailsTemp);
            Context.SubmitChanges();
            result = true;
        }
        catch
        {
            result = true;
        }
        return result;
    }
    public bool Delete(long id, long processId)
    {
        bool result = true;
        List<int> groups = new List<int>();
        try
        {
            var details = (from l in Context.StageDetails where l.StageId == id select l);
            Context.StageDetails.DeleteAllOnSubmit(details);
            var toDelete = (from l in Context.Stages where l.Id == id && l.ProcessId== processId select l);
            Context.Stages.DeleteAllOnSubmit(toDelete);
            Context.SubmitChanges();
        }
        catch
        {
            result = false;
        }
        return result;
    }
}